package com.google.webmusic.ws;

import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import com.google.webmusic.model.Member;


public class WSUserManager {
	private WSUserManager(){}
	
	/**
	 * member's socket
	 */
	private static final Map<Member, Socket> sockets = new HashMap<Member, Socket>();
	/**
	 * room's members
	 */
	private static final Map<String, Set<Member>> members = new HashMap<String, Set<Member>>();
	
	public static Socket getMemberSocket(Member member) {
		return sockets.get(member);
	}
	public static void addMember(Socket socket, Member member) {
		sockets.put(member, socket);
		Set<Member> memberSet=members.get(member.getR());
		if(memberSet==null){
			memberSet=new HashSet<Member>();
			members.put(member.getR(), memberSet);
		}
		memberSet.add(member);
	}
	public static Set<Member> getRoomMembers(Member m) {
		Set<Member> set = members.get(m);
		if(set==null) return new HashSet<Member>();
		return set;
	}
	public static void offlineMember(Member member) {
		try {
			Socket s=sockets.get(member);
			s.session.close();
			sockets.remove(member);
			
			members.get(member.getR()).remove(member);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
